import time
import os
import sys
import subprocess

while(True):
    jobs = db(db.jobs.status != 'completed').select()
    if len(jobs) == 0:
        print 'no uncompleted jobs'
    else:
        print 'processing %d jobs' % len(jobs)
        for j in jobs:
            script_file = os.path.join(os.path.join(os.path.join(sys.path[0], request.folder),'uploads'),j.script)
            stdout_file = script_file + '.out'
            stderr_file = script_file + '.err'
            if j.status == 'ready':
                print 'submitting id %d: %s %s %s' % (j.id, script_file, stdout_file, stderr_file)
                p = subprocess.Popen(args=('qsub','-e',stderr_file,'-o',stdout_file,script_file),stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
                p.wait()
                output = p.stdout.readlines()
                job_id = output[0].strip()
                print 'queued with job_id=%s' % job_id
                j.update_record(status='running',job_id=job_id)
            elif j.status == 'running':
                p = subprocess.Popen(args=['qstat',j.job_id],stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
                p.wait()
                output = p.stdout.readlines()[0].strip()
                if output == 'qstat: Unknown Job Id %s' % j.job_id:
                    print 'job id %s is no longer running' % j.job_id
                    j.update_record(status='completed',stdout=j.script + '.out',stderr=j.script + '.err')
                else:
                    print 'job id %s is still running' % j.job_id
    db.commit()
    time.sleep(5)
